package cn.tedu.rms.comment.service.impl;

import cn.tedu.rms.base.enums.OrdersStatusEnum;
import cn.tedu.rms.comment.mapper.CommentMapper;
import cn.tedu.rms.comment.pojo.dto.CommentSaveParam;
import cn.tedu.rms.comment.pojo.entity.Comment;
import cn.tedu.rms.comment.pojo.vo.CommentVO;
import cn.tedu.rms.comment.service.CommentService;
import cn.tedu.rms.orders.mapper.OrdersMapper;
import cn.tedu.rms.orders.pojo.dto.OrdersSaveParam;
import cn.tedu.rms.orders.pojo.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public void saveComment(CommentSaveParam commentSaveParam) {
        log.debug("保存评论业务，参数：{}",commentSaveParam);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentSaveParam,comment);
        // 通过是否有id判断是保存还是修改
        if (comment.getId()!=null){
            log.debug("修改评论");
            comment.setUpdateTime(new Date());
            commentMapper.updateComment(comment);
        }else {
            log.debug("新增评论");
            comment.setStatus("0");//评论状态 启用
            comment.setCreateTime(new Date());
            commentMapper.insert(comment);
            // 更新订单状态为已评论
            Orders orders = new Orders();
            orders.setId(commentSaveParam.getOrdersId());
            orders.setStatus(OrdersStatusEnum.STATUS_COMMENTED.getCode());
            ordersMapper.updateOrders(orders);
        }

    }

    @Override
    public void deleteCommentById(Long id) {
        log.debug("删除评论业务，参数：{}",id);
        commentMapper.deleteCommentById(id);
    }

    @Override
    public List<CommentVO> selectMyComment(Long userId) {
        log.debug("查询我的评论业务，参数：{}",userId);
        return commentMapper.selectMyComment(userId);
    }
}
